<template>
    <el-dialog v-model="dialogFormVisible" :title="title" @close="closeDialog">
        <el-form :model="addApprovalSealForm" ref="addApprovalSealRef" :rules="addApprovalSealrules">
            <el-form-item label="印章名称" :label-width="formLabelWidth" prop="name">
                <el-input v-model="addApprovalSealForm.name" autocomplete="off" ref="name" placeholder="请填写印章名称"></el-input>
            </el-form-item>
            <!-- <el-form-item label="管理员姓名" :label-width="formLabelWidth"
                                      prop="managerName">
                            <el-input
                                    v-model="addApprovalSealForm.managerName"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写管理员姓名"
                            ></el-input>
                        </el-form-item> -->
            <el-form-item label="印章类型" :label-width="formLabelWidth" prop="type">
                <el-select v-model="addApprovalSealForm.type" class="m-2" placeholder="请选择印章类型，0：公章，1：私章" style="width: 100%;">
                    <el-option v-for="item in sealType" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
            </el-form-item>
            <!-- <el-form-item label="管理员工号" :label-width="formLabelWidth"
                                      prop="managerNumber">
                            <el-input
                                    v-model="addApprovalSealForm.managerNumber"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写管理员工号"
                            ></el-input>
                        </el-form-item> -->
            <!-- <el-form-item label="创建时间" :label-width="formLabelWidth"
                                      prop="createdAt" v-if="addApprovalSealForm.id">
                            <el-input
                                    v-model="addApprovalSealForm.createdAt"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写创建时间"
                            ></el-input>
                        </el-form-item> -->
            <el-form-item label="印章权重" :label-width="formLabelWidth" prop="sortNum">
                <el-input v-model="addApprovalSealForm.sortNum" autocomplete="off" ref="name"
                    placeholder="请填写印章权重"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="cancel()">取消</el-button>
                <el-button type="primary" @click="save()">保存</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script>
import { ref } from "vue";
import { httpService } from "@/utils/httpService";
import { ElMessage, ElForm } from "element-plus";
import { vxRule } from "@/utils/validator";

export default {
    name: "AddApprovalSeal",
    components: { ElForm, ElMessage },
    data() {
        return {
            sealType: [
                { id: 0, name: '公章' },
                { id: 1, name: '私章' },
            ],
            dialogFormVisible: ref(false),
            formLabelWidth: "120px",
            title: "",
            personnelType: "",
            addApprovalSealForm: {
                name: "",
                type: "",
                sortNum: "",
                managerName: "",
                managerNumber: "",
                createdAt: "",
            },
            // 校验规则
            addApprovalSealrules: {
                name: vxRule(true, null, "blur"),
                type: vxRule(true, null, "blur"),
                sortNum: vxRule(true, null, "blur"),
            },
        };
    },
    methods: {
        closeDialog() {
            this.clear();
        },
        cancel() {
            this.dialogFormVisible = false;
            this.clear();
        },
        getApprovalSealById(id) {
            httpService("/javaApi/approvalSeal/queryById", { id: id }, "get").then(
                (data) => {
                    console.log(data);
                    this.addApprovalSealForm = data.approvalSeal;
                }
            );
        },
        clear() {
            this.dialogFormVisible = false;
            //清空form
            this.addApprovalSealForm.id = "";
            this.addApprovalSealForm.name = "";
            this.addApprovalSealForm.type = "";
            this.addApprovalSealForm.sortNum = "";
            this.addApprovalSealForm.managerName = "";
            this.addApprovalSealForm.managerNumber = "";
            this.addApprovalSealForm.createdAt = "";
        },
        save() {
            this.$refs.addApprovalSealRef.validate((valid) => {
                if (valid) {
                    let params = new FormData(); //创建一个form对象,必须是form对象否则后端接受不到数据
                    params.append("name", this.addApprovalSealForm.name);
                    params.append("type", this.addApprovalSealForm.type);
                    params.append("sortNum", this.addApprovalSealForm.sortNum);
                    // params.append("managerName", this.addApprovalSealForm.managerName);
                    // params.append("managerNumber", this.addApprovalSealForm.managerNumber);
                    // params.append("createdAt", this.addApprovalSealForm.createdAt);
                    if (this.addApprovalSealForm.id) {
                        params.append("id", this.addApprovalSealForm.id);
                        httpService("/javaApi/approvalSeal/edit", params, "post").then(
                            (data) => {
                                if (data.code == 200) {
                                    ElMessage.success("修改成功");
                                    this.clear();
                                    //刷新父组件的数据
                                    this.$parent.getApprovalSealList();
                                } else {
                                    ElMessage.error("修改失败");
                                }
                            }
                        );
                    } else {
                        httpService("/javaApi/approvalSeal/add", params, "post").then(
                            (data) => {
                                if (data.code == 200) {
                                    ElMessage.success("添加成功");
                                    this.clear();
                                    //刷新父组件的数据
                                    this.$parent.getApprovalSealList();
                                } else {
                                    ElMessage.error("添加失败");
                                }
                            }
                        );
                    }
                } else {
                    return false;
                }
            });
        },
    },
};
</script>
<style></style>